home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / clang / ebksrc.zip / PICKLIST.CPP < prev    next >
C/C++ Source or Header  |  1991-07-31  |  7KB  |  312 lines

  1. /*
  2.     picklist.cpp
  3.     7-30-91
  4.     Demo of coding with FlexList II
  5.  
  6.     Copyright 1991
  7.     John W. Small
  8.     All rights reserved
  9.  
  10.     Licensed users of FlexList are free to use and
  11.     modify this tool for use in their programs.
  12.  
  13.     PSW / Power SoftWare
  14.     P.O. Box 10072
  15.     McLean, VA 22102 8072 USA
  16.  
  17.     Voice: (703) 759-3838
  18.     CIS: 73757,2233
  19.  
  20. */
  21.  
  22.  
  23. #include <ctype.h>
  24. #include <pckey.hpp>
  25. #include <picklist.hpp>
  26.  
  27.  
  28. Palette PickList::defaultP = {
  29.  
  30.     0,  /*  monochrome         or         color  */
  31.  
  32.     svideo(BLACK,LIGHTGRAY),    svideo(BLUE,LIGHTGRAY),
  33.     svideo(LIGHTGRAY,BLACK),    svideo(WHITE,BLUE),
  34.     svideo(WHITE,BLACK),        svideo(DARKGRAY,RED),
  35.     svideo(DARKGRAY,LIGHTGRAY),    svideo(LIGHTGRAY,BLACK)
  36. };
  37.  
  38.  
  39. unsigned PickList::choose(unsigned curRow)
  40. {
  41.     unsigned startRow, pgLen, winHeight, winWidth;
  42.     unsigned oldCurRow, oldStartRow;
  43.     struct text_info ti;
  44.     int c, y, owscroll, pfLen;
  45.     void * D;
  46.     CursorShape cursor;
  47.  
  48.     if (!Nodes())
  49.         return 0;
  50.     owscroll = _wscroll;
  51.     _wscroll = 0;
  52.     cursor.saveOrig();
  53.     gettextinfo(&ti);
  54.     mappalette(P);
  55.  
  56.     winHeight = ti.winbottom - ti.wintop;
  57.     winWidth = ti.winright - ti.winleft + 1;
  58.     if (!status)
  59.         winHeight++;
  60.     if (winHeight <= 2)
  61.         return 0;
  62.     pgLen = winHeight - 1;
  63.  
  64.     startRow = oldCurRow = oldStartRow = 0;
  65.  
  66.  
  67.     for (c = (curRow? 0 : -Home); ; c = PCK.getkey()) {
  68.         switch (c)  {
  69.         case -PgDn:
  70.             curRow += pgLen;
  71.             if (curRow >= Nodes())  {
  72.                 curRow = Nodes();
  73.                 if (startRow + pgLen > curRow)
  74.                     break;
  75.             }
  76.             startRow += pgLen;
  77.             if (startRow > curRow)
  78.                 startRow = curRow;
  79.             prefix[pfLen = 0] = '\0';
  80.             break;
  81.         case -PgUp:
  82.             if (curRow > pgLen)
  83.                 curRow -= pgLen;
  84.             else
  85.                 curRow = 1;
  86.             if (startRow > pgLen)
  87.                 startRow -= pgLen;
  88.             else
  89.                 startRow = 1;
  90.             prefix[pfLen = 0] = '\0';
  91.             break;
  92.         case -Home:
  93.             curRow = startRow = 1;
  94.             prefix[pfLen = 0] = '\0';
  95.             break;
  96.         case -EndKey:
  97.             curRow = Nodes();
  98.             if (curRow > startRow + pgLen)
  99.                 if (curRow > pgLen/2)
  100.                     startRow = curRow
  101.                         - pgLen/2;
  102.                 else
  103.                     startRow = 1;
  104.             prefix[pfLen = 0] = '\0';
  105.             break;
  106.         case -UpArr:
  107.             if (curRow > 1)
  108.                 if (--curRow < startRow)
  109.                     startRow = curRow;
  110.             prefix[pfLen = 0] = '\0';
  111.             break;
  112.         case -DnArr:
  113.             if (curRow < Nodes())  {
  114.                 if (++curRow - startRow > pgLen)
  115.                     startRow++;
  116.             }
  117.             else if (startRow < curRow)
  118.                 startRow++;
  119.             prefix[pfLen = 0] = '\0';
  120.             break;
  121.         case 0: // start display at other than line one
  122.             prefix[pfLen = 0] = '\0';
  123.             if (curRow <= Nodes())  {
  124.                 startRow = 1;
  125.                 if (curRow > startRow + pgLen)  {
  126.                     if (curRow > pgLen/2)
  127.                         startRow = curRow
  128.                             - pgLen/2;
  129.                     else
  130.                         startRow = curRow;
  131.                 }
  132.                 break;
  133.             }
  134.             // fall thru to ESC
  135.         case ESC:
  136.             curRow = 0;
  137.             // fall thru to CR
  138.         case CR:
  139.             if ((D = mkcur(curRow)) != (void *)0)
  140.                 if ((y = doIt(D)) > 0)  {
  141.                     // keep same display
  142.                     oldCurRow = oldStartRow = 0;
  143.                     break;
  144.                 }
  145.                 else if (y < 0)  {
  146.                     // new display
  147.                     oldCurRow = oldStartRow = 0;
  148.                     curRow = startRow = 1;
  149.                     prefix[pfLen = 0] = '\0';
  150.                     break;
  151.                 }
  152.             textattr(ti.attribute);
  153.             _wscroll = owscroll;
  154.             cursor.restoreOrig();
  155.             return CurNum();
  156.         case BACKSP:
  157.             if (!pfLen)
  158.                 continue;
  159.             prefix[--pfLen] = '\0';
  160.             if (!pfLen)  {
  161.                 curRow = startRow = 1;
  162.                 break;
  163.             }
  164.             mkcur();
  165.             while ((D = nextD()) != (void *)0)
  166.                 if (match(prefix,D))  {
  167.                     curRow = CurNum();
  168.                     if (curRow > startRow + pgLen ||
  169.                         curRow < startRow)
  170.                         if (curRow > pgLen/2)
  171.                             startRow = curRow - pgLen/2;
  172.                         else
  173.                             startRow = 1;
  174.                     break;
  175.                 }
  176.             break;
  177.         default:
  178.             if (c < 0 || !isprint(c))  {
  179.                 if ((D = mkcur(curRow)) != (void *)0)
  180.                     if (idle(c))  {
  181.                         // new display
  182.                         oldCurRow = oldStartRow = 0;
  183.                         curRow = startRow = 1;
  184.                         prefix[pfLen = 0] = '\0';
  185.                         break;
  186.                     }
  187.                 continue;
  188.             }
  189.             if (pfLen >= MAX_PL_PREFIX)
  190.                 continue;
  191.             prefix[pfLen++] = toupper(c);
  192.             prefix[pfLen] = '\0';
  193.             mkcur();
  194.             while ((D = nextD()) != (void *)0)
  195.                 if (match(prefix,D))  {
  196.                     curRow = CurNum();
  197.                     if (curRow > startRow + pgLen ||
  198.                         curRow < startRow)
  199.                         if (curRow > pgLen/2)
  200.                             startRow = curRow - pgLen/2;
  201.                         else
  202.                             startRow = 1;
  203.                     break;
  204.                 }
  205.             if (!CurNum())
  206.                 prefix[pfLen--] = '\0';
  207.             break;
  208.         }
  209.         cursor.off();
  210.         if (oldStartRow && ((((y = oldStartRow-startRow) > 0)?
  211.             y : -y) <= 1))  {
  212.             mapvideo(NORMAL,P);
  213.             if (oldCurRow && oldCurRow != curRow)  {
  214.                 gotoxy(1,oldCurRow - oldStartRow + 1);
  215.                 displayLine(mkcur(oldCurRow),winWidth);
  216.             }
  217.             if (y != 0)  {
  218.                 window(ti.winleft,ti.wintop,
  219.                     ti.winright,ti.wintop+winHeight-1);
  220.                 if (y > 0)  /* scroll up */
  221.                     insline();
  222.                 else  /* scroll down */
  223.                     delline();
  224.                 window(ti.winleft,ti.wintop,
  225.                     ti.winright,ti.winbottom);
  226.             }
  227.             if (oldCurRow != curRow)  {
  228.                 gotoxy(1,curRow - startRow + 1);
  229.                 mapvideo(SELECT,P);
  230.                 displayLine(mkcur(curRow),winWidth,1);
  231.             }
  232.         }
  233.         else  {
  234.             mapvideo(NORMAL,P);
  235.             for (y = 1, D = mkcur(startRow);
  236.                 y <= winHeight && D;
  237.                 y++, D = nextD((void *)0))  {
  238.                 gotoxy(1,y);
  239.                 if (CurNum() == curRow)  {
  240.                     mapvideo(SELECT,P);
  241.                     displayLine(D,winWidth,1);
  242.                     mapvideo(NORMAL,P);
  243.                 }
  244.                 else  {
  245.                     displayLine(D,winWidth);
  246.                 }
  247.             }
  248.             while (y <= winHeight)  {
  249.                 gotoxy(1,y++);
  250.                 clreol();
  251.             }
  252.         }
  253.         if (status) if (oldCurRow != curRow)  {
  254.             mapvideo(STATUS,P);
  255.             gotoxy(1,winHeight+1);
  256.             displayStatus(mkcur(curRow),winWidth);
  257.         }
  258.         oldCurRow = curRow;
  259.         oldStartRow = startRow;
  260.         if (pfLen)  {
  261.             gotoxy(xcursor(mkcur(curRow),prefix,pfLen),
  262.                 curRow - startRow + 1);
  263.             cursor.block();
  264.         }
  265.         else  {
  266.             gotoxy(1,curRow - startRow + 1);
  267.             cursor.normal();
  268.         }
  269.     }
  270.  
  271. }
  272.  
  273. int PickList::subtractD(void * D)
  274. {
  275.     if (findFirstD(D))
  276.         return delD();
  277.     return 0;
  278. }
  279.  
  280. unsigned PickListWindow::choose(int left, int top,
  281.     int right, int bottom, unsigned curRow)
  282. {
  283.     if (!window(left,top,right,bottom))
  284.         return 0;
  285.     dressWindow();
  286.     unsigned ok = PickList::choose(curRow);
  287.     close();
  288.     return ok;
  289. }
  290.  
  291. unsigned PickListWindow::choose(unsigned curRow)
  292. {
  293.     struct text_info ti;
  294.     int l, t, r, b, w, h;
  295.  
  296.  
  297.     w = paneWidth();
  298.     gettextinfo(&ti);
  299.     if (w > ti.screenwidth)
  300.         w = ti.screenwidth;
  301.     else if (!w)
  302.         w = ti.screenwidth/2;
  303.     h = ti.screenheight/6 * 4;
  304.     l = (ti.screenwidth - w)/2;
  305.     r = l + w;
  306.     t = ti.screenheight/6;
  307.     b = t + h;
  308.     return choose(l,t,r,b,curRow);
  309. }
  310.  
  311.  
  312.